package com.rjwh.dingdong.client.activity.more;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.webbrowser.WebActivity;
import com.rjwh.dingdong.client.bean.localbean.ScoreProfile;
import com.rjwh.dingdong.client.mvp.presenter.impl.MorePointsPresenter;
import com.rjwh.dingdong.client.mvp.view.MorePointsView;
import com.rjwh.dingdong.client.util.LogUtil;
import com.rjwh.dingdong.client.util.UrlUtil;
import com.rjwh.dingdong.client.view.FloatTextProgressBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 我的积分
 *
 * @author Kay
 * @ClassName MorePointsActivity
 * @Description
 * @date 2016-9-21
 * @history 2016-9-21 author: description:
 */
public class MorePointsActivity extends BaseActivity implements MorePointsView {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.more_points_progressbar)
    FloatTextProgressBar morePointsProgressbar;
    @BindView(R.id.more_points_current_level_tv)
    TextView morePointsCurrentLevelTv;
    @BindView(R.id.more_points_start_img)
    ImageView morePointsStartImg;
    @BindView(R.id.more_points_start_tv)
    TextView morePointsStartTv;
    @BindView(R.id.more_points_end_tv)
    TextView morePointsEndTv;
    @BindView(R.id.more_points_end_img)
    ImageView morePointsEndImg;
    @BindView(R.id.more_points_detial_tv)
    TextView morePointsDetialTv;
    @BindView(R.id.loading_layout)
    RelativeLayout loadingLayout;
    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    @BindView(R.id.more_points_rule_root)
    RelativeLayout morePointsRuleRoot;
    @BindView(R.id.more_points_earn_root)
    RelativeLayout morePointsEarnRoot;

    private MorePointsPresenter presenter;
    private String scorepage;
    private String scorerule;
    private String totalscore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_points_layout);
        ButterKnife.bind(this);
        initView();
        presenter = new MorePointsPresenter(this, this);
        doGetMyScorePage();
    }

    private void doGetMyScorePage() {
        presenter.doGetMyScorePage();
    }

    private void initView() {
        initToolbarHelper();
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(getResources().getString(R.string.more_my_points));
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    private void setLevelIcon(ImageView imageView, int level) {
        int id = 0;
        switch (level) {
            case 1:
                id = R.drawable.icon_level_v1;
                break;
            case 2:
                id = R.drawable.icon_level_v2;
                break;
            case 3:
                id = R.drawable.icon_level_v3;
                break;
            case 4:
                id = R.drawable.icon_level_v4;
                break;
            case 5:
                id = R.drawable.icon_level_v5;
                break;
            case 6:
                id = R.drawable.icon_level_v6;
                break;
            default:
                break;
        }
        imageView.setImageResource(id);
    }

    @OnClick({R.id.more_points_detial_root, R.id.more_points_rule_root, R.id.more_points_earn_root, R.id.btn_refresh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more_points_detial_root:
                MorePointsDetialActivity.startActivity(MorePointsActivity.this, totalscore);
                break;
            case R.id.more_points_rule_root:
                WebActivity.startActivity(MorePointsActivity.this, scorerule);
                break;
            case R.id.more_points_earn_root:
                String webUrl = UrlUtil.getWebUrl(this, scorepage);
                WebActivity.startActivity(MorePointsActivity.this, webUrl);
                break;
            case R.id.btn_refresh:
                doGetMyScorePage();
                break;
        }
    }

    @Override
    public void showLoadingView() {
        if (loadingLayout != null) {
            loadingLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideLoadingView() {
        if (loadingLayout != null) {
            loadingLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void showErrorView() {
        if (errorLayout != null) {
            errorLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideErrorView() {
        if (errorLayout != null) {
            errorLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void loadData(ScoreProfile scoreprofile) {
        String currentlevel = scoreprofile.getCurrentlevel();
        String currentscore = scoreprofile.getCurrentscore();
        String beginscore = scoreprofile.getBeginscore();
        String endscore = scoreprofile.getEndscore();
        totalscore = scoreprofile.getTotalscore();
        scorerule = scoreprofile.getScorerule();
        scorepage = scoreprofile.getScorepage();
        if (AsStrUtils.isEmpty(scorerule)) {
            morePointsRuleRoot.setVisibility(View.GONE);
        } else {
            morePointsRuleRoot.setVisibility(View.VISIBLE);
        }
        if (AsStrUtils.isEmpty(scorepage)) {
            morePointsEarnRoot.setVisibility(View.GONE);
        } else {
            morePointsEarnRoot.setVisibility(View.VISIBLE);
        }
        if (!AsStrUtils.isEmpty(beginscore) && !AsStrUtils.isEmpty(endscore) && !AsStrUtils.isEmpty(currentlevel) && !AsStrUtils.isEmpty(totalscore)) {
            morePointsStartTv.setText(beginscore);
            morePointsEndTv.setText(endscore);
            morePointsCurrentLevelTv.setText(String.format(getString(R.string.more_points_current_level), currentlevel));
            morePointsDetialTv.setText(totalscore);
            setLevelIcon(morePointsStartImg, Integer.parseInt(currentlevel));
            setLevelIcon(morePointsEndImg, Integer.parseInt(currentlevel) + 1);
            float curScoreInt = Float.parseFloat(currentscore);
            float begScoreInt = Float.parseFloat(beginscore);
            float endScoreInt = Float.parseFloat(endscore);
            float progress = (curScoreInt - begScoreInt) / (endScoreInt - begScoreInt) * 100;
            LogUtil.d("MorePointsActivity", "curScoreInt:" + curScoreInt);
            LogUtil.d("MorePointsActivity", "begScoreInt:" + begScoreInt);
            LogUtil.d("MorePointsActivity", "endScoreInt:" + endScoreInt);
            LogUtil.d("MorePointsActivity", "progress:" + progress);
            morePointsProgressbar.setProgress(progress);
            morePointsProgressbar.setProgressCount(Integer.parseInt(currentscore));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
